import { request } from '@/utils/request';

// 设备状态枚举
export enum DeviceStatus {
  ONLINE = 'online',
  OFFLINE = 'offline',
  ERROR = 'error',
  MAINTENANCE = 'maintenance',
}

// 设备类型枚举
export enum DeviceType {
  LOCK = 'lock',
  METER = 'meter',
  CAMERA = 'camera',
  SENSOR = 'sensor',
  CONTROLLER = 'controller',
  LIGHTING = 'lighting',
  AC = 'ac',
  CURTAIN = 'curtain',
}

// 告警级别枚举
export enum AlertLevel {
  CRITICAL = 'critical',
  WARNING = 'warning',
  INFO = 'info',
}

// 告警状态枚举
export enum AlertStatus {
  ACTIVE = 'active',
  ACKNOWLEDGED = 'acknowledged',
  RESOLVED = 'resolved',
}

// 设备接口
export interface IOTDevice {
  id: string;
  name: string;
  deviceId: string;
  type: DeviceType;
  status: DeviceStatus;
  location: string;
  lastUpdate: string;
  model: string;
  manufacturer: string;
  ipAddress: string;
  port: string;
  battery?: number;
  signal?: number;
  firmware: string;
  installDate: string;
  warranty: string;
  description: string;
}

// 设备控制接口
export interface DeviceControl {
  id: string;
  name: string;
  type: string;
  status: DeviceStatus;
  controls: ControlItem[];
  lastUpdate: string;
}

// 控制项接口
export interface ControlItem {
  id: string;
  name: string;
  type: 'switch' | 'slider' | 'button' | 'display';
  value: any;
  min?: number;
  max?: number;
  unit?: string;
  options?: { label: string; value: any }[];
}

// 告警接口
export interface Alert {
  id: string;
  title: string;
  description: string;
  level: AlertLevel;
  deviceId: string;
  deviceName: string;
  deviceType: DeviceType;
  location: string;
  timestamp: string;
  status: AlertStatus;
  assignee?: string;
  notes?: string;
}

// 维护记录接口
export interface MaintenanceRecord {
  id: string;
  deviceId: string;
  date: string;
  type: string;
  description: string;
  technician: string;
  status: 'completed' | 'in_progress';
}

// 设备统计接口
export interface DeviceStatistics {
  totalDevices: number;
  totalChange: number;
  onlineDevices: number;
  onlineChange: number;
  offlineDevices: number;
  offlineChange: number;
  faultDevices: number;
  faultChange: number;
}

// 查询参数接口
export interface DeviceQueryParams {
  name?: string;
  type?: DeviceType;
  status?: DeviceStatus;
  location?: string;
  page?: number;
  pageSize?: number;
}

// 告警查询参数接口
export interface AlertQueryParams {
  level?: AlertLevel;
  status?: AlertStatus;
  deviceType?: DeviceType;
  deviceId?: string;
  page?: number;
  pageSize?: number;
}

// 获取设备列表
export function getDeviceList(params?: DeviceQueryParams) {
  return request.get<{
    list: IOTDevice[];
    total: number;
    statistics: DeviceStatistics;
  }>('/hotel/iot-devices', { params });
}

// 获取设备详情
export function getDeviceDetail(deviceId: string) {
  return request.get<IOTDevice>(`/hotel/iot-devices/${deviceId}`);
}

// 添加设备
export function addDevice(device: Partial<IOTDevice>) {
  return request.post<IOTDevice>('/hotel/iot-devices', device);
}

// 更新设备
export function updateDevice(deviceId: string, device: Partial<IOTDevice>) {
  return request.put<IOTDevice>(`/hotel/iot-devices/${deviceId}`, device);
}

// 删除设备
export function deleteDevice(deviceId: string) {
  return request.delete(`/hotel/iot-devices/${deviceId}`);
}

// 批量删除设备
export function batchDeleteDevices(deviceIds: string[]) {
  return request.delete('/hotel/iot-devices/batch', { data: { deviceIds } });
}

// 获取设备控制信息
export function getDeviceControl(deviceId: string) {
  return request.get<DeviceControl>(`/hotel/iot-devices/${deviceId}/control`);
}

// 控制设备
export function controlDevice(deviceId: string, controlId: string, value: any) {
  return request.post(`/hotel/iot-devices/${deviceId}/control`, {
    controlId,
    value,
  });
}

// 重启设备
export function restartDevice(deviceId: string) {
  return request.post(`/hotel/iot-devices/${deviceId}/restart`);
}

// 更新设备固件
export function updateDeviceFirmware(deviceId: string) {
  return request.post(`/hotel/iot-devices/${deviceId}/firmware/update`);
}

// 获取设备状态历史
export function getDeviceStatusHistory(deviceId: string, days: number = 7) {
  return request.get(`/hotel/iot-devices/${deviceId}/status-history`, {
    params: { days },
  });
}

// 获取告警列表
export function getAlertList(params?: AlertQueryParams) {
  return request.get<{
    list: Alert[];
    total: number;
  }>('/hotel/iot-devices/alerts', { params });
}

// 获取告警详情
export function getAlertDetail(alertId: string) {
  return request.get<Alert>(`/hotel/iot-devices/alerts/${alertId}`);
}

// 确认告警
export function acknowledgeAlert(alertId: string) {
  return request.post(`/hotel/iot-devices/alerts/${alertId}/acknowledge`);
}

// 解决告警
export function resolveAlert(alertId: string, notes?: string) {
  return request.post(`/hotel/iot-devices/alerts/${alertId}/resolve`, {
    notes,
  });
}

// 批量确认告警
export function batchAcknowledgeAlerts(alertIds: string[]) {
  return request.post('/hotel/iot-devices/alerts/batch-acknowledge', {
    alertIds,
  });
}

// 批量解决告警
export function batchResolveAlerts(alertIds: string[]) {
  return request.post('/hotel/iot-devices/alerts/batch-resolve', {
    alertIds,
  });
}

// 获取维护记录
export function getMaintenanceRecords(deviceId: string) {
  return request.get<MaintenanceRecord[]>(
    `/hotel/iot-devices/${deviceId}/maintenance`,
  );
}

// 添加维护记录
export function addMaintenanceRecord(
  deviceId: string,
  record: Partial<MaintenanceRecord>,
) {
  return request.post<MaintenanceRecord>(
    `/hotel/iot-devices/${deviceId}/maintenance`,
    record,
  );
}

// 获取设备统计数据
export function getDeviceStatistics() {
  return request.get<DeviceStatistics>('/hotel/iot-devices/statistics');
}

// 测试设备连接
export function testDeviceConnection(deviceId: string) {
  return request.post(`/hotel/iot-devices/${deviceId}/test`);
}

// 批量测试设备连接
export function batchTestDevices(deviceIds: string[]) {
  return request.post('/hotel/iot-devices/batch-test', { deviceIds });
}

// 导出设备列表
export function exportDeviceList(params?: DeviceQueryParams) {
  return request.get('/hotel/iot-devices/export', {
    params,
    responseType: 'blob',
  });
}

// 导入设备列表
export function importDeviceList(file: File) {
  const formData = new FormData();
  formData.append('file', file);
  return request.post('/hotel/iot-devices/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
}
